import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

let gjsj;
let sy = document.getElementById('sy');
sy.addEventListener('click', () => {
  location.href = './twtwz.html';
});

//#region 个人信息
function geturl() {
  let params = location.search.length > 0 ? location.search.substring(1) : '',
    args = {},
    items = params.length ? params.split('&') : [],
    item = '',
    key = '',
    value = '',
    len = items.length;

  for (let i = 0; i < len; i++) {
    item = items[i].split('=');
    key = decodeURIComponent(items[0]);
    value = decodeURIComponent(item[1]);
  
    gjsj = value;

    if (key.length) {
      args[key] = value;
    }
  }

  return args;
}

geturl();
console.log('id',gjsj);

let info = {
  orderBy: 1,
  umid: gjsj
};
let page = {
  pageNumber: 1,
  pageSize: 4
};
let cs = tools.concatJson(info, page);

let sz = [];
//用户基本信息
let sz1 = [];
function prlb() {
  ajax.send('/message/queryDetail', cs, (data) => {
    if (data.success) {
      sz = data.list;
      sz1 = data.info;
      page = data.page;
      zs();
    }
  });
}
//页面展示
function zs() {
    
  for (let i = 0; i < sz.length; i++) {
    const x = sz[i];
  let imgLogo1 = document.getElementById('imgLogo1');
    imgLogo1.src = `${x.userInfo.img}`;

    let imgLogo2 = document.getElementById('imgLogo2');
    imgLogo2.src = `${x.userInfo.img}`;

    let imgLogo = document.getElementById('imgLogo');
    imgLogo.src = `${x.userInfo.img}`;


    let zhh = document.getElementById('zhh');
    zhh.innerHTML = x.user.nickname;

    let drm = document.getElementById('drm');
    drm.value = x.user.username;

    let drm1=document.getElementById('drm1')
    drm1.value = x.user.username;

  let datas=document.getElementById('datas');
  datas.innerHTML="";
  }
}

//#region 添加好友
let tjhyDialog = document.getElementById('tjhyDialog');
let btnadd = document.getElementById('btnadd');
btnadd.addEventListener('click', () => {
  let cs = {
    username: drm.value
  };
  console.log('csss',cs.username)
  ajax.send('/user/auth/friend', cs, (data) => {
    showToast(data.message);
    if (data.success) {
      
      drm.focus();
    }
  });
});

tjhyDialog.addEventListener('click', () => {
  // tjhaoyou();
});
//#endregion


//#region 关注
let gzDialog = document.getElementById('gzDialog');
let btngz = document.getElementById('btngz');
btngz.addEventListener('click', () => {
  let cs = {
    username: drm.value
  };
  console.log('css',cs.username)
  ajax.send('/message/followUser', cs, (data) => {
    showToast(data.message);
    if (data.success) {
      drm.focus();
    }
  });
});

gzDialog.addEventListener('click', () => {
  // tjhaoyou();
});


//#endregion


//#region 个人评论展示
let cs1 = {
  username: drm.value,
  orderBy: 1
};
console.log('zzz', cs1.username);
let cs2 = {
  pageNumber: 1,
  pageSize: 3
};
let cs3 = [];
function plzs() {
  ajax.send(
    '/message/queryReplyByUsername',
    tools.concatJson(cs1, cs2),
    (data) => {
      if (data.success) {
        cs3 = data.list;
        cs2 = data.page;
      }
    }
  );
}
//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

// tjhaoyou();
prlb();
plzs();
